import { http } from "@/utils/http";
import { lmsUrlApi } from "./utils";

export type Result = {
  list: Array<any>;
  total?: number;
};

type TreeNode = {
  name: string;
  type: number;
  parentId: number;
  sort: number;
  leaderUserId: number;
  phone: string;
  email: string;
  status: number;
  id: number;
  createTime: number;
  remark: string;
};

export const transitionTreeData = (r: Array<any>) => {
  const li: TreeNode[] = [];
  r.forEach(item => {
    li.push({
      name: item.name,
      type: 1,
      parentId: item.parentId,
      sort: 0,
      leaderUserId: 1,
      phone: "",
      email: "",
      status: 1,
      id: item.folderId,
      createTime: item.createdAt,
      remark: ""
    });
  });
  return li;
};

export type AddCourseware = {
  code: string;
  fileName: string;
  fileUid: string;
  width: string;
  height: string;
  label: string;
  coursewareFolderId: string;
  allowDownload: boolean;
  isPublic: boolean;
};

export type EditCourseware = {
  id: string;
  code: string;
  fileName: string;
  label: string;
  coursewareFolderId: string;
  allowDownload: boolean;
  isPublic: boolean;
};

export type SearchCourseware = {
  name: string;
  code: string;
  folderId: string;
  isMe: boolean;
  pageNum: number;
  pageSize: number;
};

export type AddCoursewareFolder = {
  name: string;
  parentId: string;
  describe: string;
};

export const GetCoursewareFolderList = () => {
  return http.request<any[]>("get", lmsUrlApi("courseware/folderList"));
};
export const CreateCoursewareFolder = (data: AddCoursewareFolder) => {
  return http.request("post", lmsUrlApi("courseware/createCoursewareFolder"), {
    data
  });
};

// 删除课件目录
export const deleteCreatewareFolder = (id: string) => {
  return http.request("delete", lmsUrlApi("courseware/folder/" + id));
};

export const UploadCourseware = (option: any) => {
  const formData = new FormData();
  // @ts-ignore
  formData.append("file", option.file);
  return http
    .post(
      lmsUrlApi("courseware/upload"),
      { data: formData },
      {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      }
    )
    .then(res => {
      return res;
    });

  // return http.request("post", lmsUrlApi("courseware/upload"), option.file);
};

// 获取推荐的课件编号
export const getCoursewareCode = () => {
  return http.request<string>("get", lmsUrlApi("courseware/code"));
};

export const CreateCourseware = (data: AddCourseware) => {
  return http.request("post", lmsUrlApi("courseware/create"), { data });
};
export const updateCourseware = (data: EditCourseware) => {
  return http.request("put", lmsUrlApi("courseware/update"), { data });
};
// 删除课件
export const deleteCourseware = (id: string) => {
  return http.request("delete", lmsUrlApi("courseware/" + id));
};
export const GetcoursewarePage = (params: SearchCourseware) => {
  return http.request<Result>("get", lmsUrlApi("courseware/page"), {
    params
  });
};
